﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model.Cuotas;
using System.Drawing;
using Model.Miembro;
using System.Windows.Forms;
using System.Web.Security;

namespace Interfaces.Cuotas
{
    public partial class RealizarCobro : System.Web.UI.Page
    {
        PagoDeCuota mensual = null;
        PagoDeCuota anual = null;
        ClaseMiembro asdf = null;
        Int64 anio;
        Int64 mes;
        string res;

        int MontoMensual = 45;
        int MontoAnual = 200;
        int MontoMensualDescuento = 35;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
                FormsAuthentication.RedirectToLoginPage();
            
            ClaseMiembro asdfa = (ClaseMiembro)Session["ClaseMiembro"];
            if (asdfa.cargo == "Tesorera")
            {

                if (Request.QueryString["va1"] == null)
                {
                    //Label5.Text = "Numero de carnet invalido";
                    MultiView1.ActiveViewIndex = 1;
                }
                else
                {
                    try
                    {
                        MultiView1.ActiveViewIndex = 0;
                        string res = Request.QueryString["va1"];
                        Int64 carnet = Convert.ToInt64(res);
                        mensual = Processes.Cuotas.GestorCuotas.MetodoBuscarCoutaMes(carnet);
                        anual = Processes.Cuotas.GestorCuotas.MetodoBuscarCuotaAnual(carnet);
                        asdf = DataBase.Miembro.BDMiembro.BuscarMiembro(carnet);
                        Label4.Text = " " + asdf.nombre + " " + asdf.apePaterno + " " + asdf.apeMaterno;
                        if (mensual != null)
                        {

                            anio = mensual.indice / 100;
                            Int64 cod = anio * 100;
                            mes = mensual.indice - cod;
                            Label2.Text = "Cancelado hasta: " + getNombreMes(mes) + " de " + anio;
                            Int64 mesPunt = mes + 1;
                            if (mesPunt <= 12)
                            {
                                for (Int64 i = 1; i <= 12; i++)
                                {
                                    nombreDeCheck(i, mesPunt);
                                    if (mesPunt == 12)
                                        mesPunt = 1;
                                    else
                                        mesPunt += 1;
                                }
                            }

                        }
                        else
                        {
                            Label2.Text = "No cuenta con ningun pago";
                            mensual = new PagoDeCuota();
                            mensual.carnet = carnet;
                            //mensual.tipo = "uno";
                            string auzzz = (DateTime.Now.Year - 1) + "12";
                            mensual.indice = Convert.ToInt32(auzzz);
                            //mensual.indice = 2;
                            anio = DateTime.Now.Year - 1;
                            mes = 12;
                            Int64 mesPunt = mes + 1;
                            if (mesPunt <= 12)
                            {
                                for (Int64 i = 1; i <= 12; i++)
                                {
                                    nombreDeCheck(i, mesPunt);
                                    if (mesPunt == 12)
                                        mesPunt = 1;
                                    else
                                        mesPunt += 1;
                                }
                            }
                        }
                        if (anual != null)
                        {
                            CheckBox14.Text = "Gestion " + (anual.indice + 1);
                        }
                        else
                        {
                            CheckBox14.Text = "Gestion " + DateTime.Now.Year;
                        }
                    }
                    catch (Exception asf)
                    {
                        //Label5.Text = "No especifico carnet";
                        MultiView1.ActiveViewIndex = 1;
                    }

                }
            }
            else
            {
                Label5.Text = "No cuenta con los permisos adecuados";
                MultiView1.ActiveViewIndex = 1;
            }

        }

        protected void Page_PreInit(object sender, EventArgs e)
        {
            if (Session["BuscarMaster"] != null)
            {
                res = (string)Session["BuscarMaster"];
                this.MasterPageFile = res;
            }
        }
        private void nombreDeCheck(Int64 indiceCheck,Int64 mespuntero)
        {
            string nombreMes = getNombreMes(mespuntero);
            switch (indiceCheck)
            {
                case 1:
                    CheckBox1.Text = nombreMes;
                    break;
                case 2:
                    CheckBox2.Text = nombreMes;
                    break;
                case 3:
                    CheckBox3.Text = nombreMes;
                    break;
                case 4:
                    CheckBox4.Text = nombreMes;
                    break;
                case 5:
                    CheckBox5.Text = nombreMes;
                    break;
                case 6:
                    CheckBox6.Text = nombreMes;
                    break;
                case 7:
                    CheckBox7.Text = nombreMes;
                    break;
                case 8:
                    CheckBox8.Text = nombreMes;
                    break;
                case 9:
                    CheckBox9.Text = nombreMes;
                    break;
                case 10:
                    CheckBox10.Text = nombreMes;
                    break;
                case 11:
                    CheckBox11.Text = nombreMes;
                    break;
                case 12:
                    CheckBox12.Text = nombreMes;
                    break;
            }
        }
        private string getNombreMes(Int64 donde)
        {
            
                switch (donde)
                {
                    case 1:
                        return  "Enero ";
                        
                    case 2:
                        return "Febrero ";
                    case 3:
                        return "Marzo " ;
                    case 4:
                        return "Abril " ;
                    case 5:
                        return "Mayo ";
                    case 6:
                        return "Junio " ;
                    case 7:
                        return "Julio " ;
                    case 8:
                        return "Agosto " ;
                    case 9:
                        return "Septiembre ";
                    case 10:
                        return "Octubre ";
                    case 11:
                        return "Noviembre " ;
                    case 12:
                        return "Diciembre " ;
                    
                }
                return "";
            }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ////====================================================================
            //int res = verificarCheck();
            //if (res > 0 || CheckBox14.Checked==true)
            //{
            //    Panel1.Enabled = false;
            //    //Panel2.Enabled = false;
            //    Panel3.Enabled = true;
            //    Panel1.BackColor = Color.White;
            //    //Panel2.BackColor = Color.White;
            //    Panel3.BackColor = Color.Silver;



            //    int aux = verificarCheck();
            //    string aux2 = "";
            //    if (CheckBox14.Checked == true)
            //    {
            //        aux2 = (anual.indice + 1).ToString();
            //    }
            //    Processes.Cuotas.GestorCuotas.realizarPago(mensual.carnet, aux.ToString(), aux2, mensual.indice);
            //    Response.Redirect("~/Miembro/Buscar.aspx");
                
            //}
            //else
            //{
            //    Label1.Text = "Seleccionar un item";
            //    Label1.Visible = true;
            //    Label3.Visible = false;
            //}
                        
        }
        private int verificarCheck()
        {
            int cont = 0;
            if (CheckBox1.Checked)
            {
                cont += 1;
            }
            if (CheckBox2.Checked)
            {
                cont += 1;
            }
            if (CheckBox3.Checked)
            {
                cont += 1;
            }
            if (CheckBox4.Checked)
            {
                cont += 1;
            }
            if (CheckBox5.Checked)
            {
                cont += 1;
            }
            if (CheckBox6.Checked)
            {
                cont += 1;
            }
            if (CheckBox7.Checked)
            {
                cont += 1;
            }
            if (CheckBox8.Checked)
            {
                cont += 1;
            }
            if (CheckBox9.Checked)
            {
                cont += 1;
            }
            if (CheckBox10.Checked)
            {
                cont += 1;
            }
            if (CheckBox11.Checked)
            {
                cont += 1;
            }
            if (CheckBox12.Checked)
            {
                cont += 1;
            }
            return cont;
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
        }

        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            Label1.Visible = false;
            CheckBox1.Enabled = false;
            CheckBox2.Enabled = true;
            CheckBox1.TextAlign = TextAlign.Right;
            CheckBox2.TextAlign = TextAlign.Left;
            TextBox1.Text = (Convert.ToInt32(TextBox1.Text) + MontoMensual).ToString();
        }

        protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBox2.Checked == true)
            {
                CheckBox2.Enabled = false;
                CheckBox3.Enabled = true;
                CheckBox2.TextAlign = TextAlign.Right;
                CheckBox3.TextAlign = TextAlign.Left;
                TextBox1.Text = (Convert.ToInt32(TextBox1.Text) + MontoMensual).ToString();
            }
            
            
        }

        protected void CheckBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBox3.Checked == true)
            {
                CheckBox3.TextAlign = TextAlign.Right;
                CheckBox4.TextAlign = TextAlign.Left;
                CheckBox3.Enabled = false;
                CheckBox4.Enabled = true;
                TextBox1.Text = (Convert.ToInt32(TextBox1.Text) + MontoMensual).ToString();
            }
           
        }

        protected void CheckBox4_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBox4.Checked == true)
            {
                CheckBox4.TextAlign = TextAlign.Right;
                CheckBox5.TextAlign = TextAlign.Left;
                CheckBox4.Enabled = false;
                CheckBox5.Enabled = true;
                TextBox1.Text = (Convert.ToInt32(TextBox1.Text) + MontoMensual).ToString();
            }
           
        }

        protected void CheckBox5_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBox5.Checked == true)
            {
                CheckBox5.TextAlign = TextAlign.Right;
                CheckBox6.TextAlign = TextAlign.Left;
                CheckBox5.Enabled = false;
                CheckBox6.Enabled = true;
                TextBox1.Text = (Convert.ToInt32(TextBox1.Text) + MontoMensual).ToString();
            }
           
        }

        protected void CheckBox6_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBox6.Checked == true)
            {
                CheckBox6.TextAlign = TextAlign.Right;
                CheckBox7.TextAlign = TextAlign.Left;
                CheckBox6.Enabled = false;
                CheckBox7.Enabled = true;
                TextBox1.Text = (Convert.ToInt32(TextBox1.Text) + MontoMensual).ToString();
            }
            
        }

        protected void CheckBox7_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBox7.Checked == true)
            {
                CheckBox7.TextAlign = TextAlign.Right;
                CheckBox8.TextAlign = TextAlign.Left;
                CheckBox7.Enabled = false;
                CheckBox8.Enabled = true;
                TextBox1.Text = (Convert.ToInt32(TextBox1.Text) + MontoMensual).ToString();
            }
            
        }

        protected void CheckBox8_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBox8.Checked == true)
            {
                CheckBox8.TextAlign = TextAlign.Right;
                CheckBox9.TextAlign = TextAlign.Left;
                CheckBox8.Enabled = false;
                CheckBox9.Enabled = true;
                TextBox1.Text = (Convert.ToInt32(TextBox1.Text) + MontoMensual).ToString();
            }
            
        }

        protected void CheckBox9_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBox9.Checked == true)
            {
                CheckBox9.TextAlign = TextAlign.Right;
                CheckBox10.TextAlign = TextAlign.Left;
                CheckBox9.Enabled = false;
                CheckBox10.Enabled = true;
                TextBox1.Text = (Convert.ToInt32(TextBox1.Text) + MontoMensual).ToString();
            }
         }

        protected void CheckBox10_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBox10.Checked == true)
            {
                CheckBox10.TextAlign = TextAlign.Right;
                CheckBox11.TextAlign = TextAlign.Left;
                CheckBox10.Enabled = false;
                CheckBox11.Enabled = true;
                TextBox1.Text = (Convert.ToInt32(TextBox1.Text) + MontoMensual).ToString();
            }
        }
        protected void CheckBox11_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBox11.Checked == true)
            {
                CheckBox11.TextAlign = TextAlign.Right;
                CheckBox12.TextAlign = TextAlign.Left;
                CheckBox11.Enabled = false;
                CheckBox12.Enabled = true;
                TextBox1.Text = (Convert.ToInt32(TextBox1.Text) + MontoMensual).ToString();
            }
        }

        protected void CheckBox12_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBox12.Checked == true)
            {
                CheckBox13.Checked = true;

                CheckBox12.Enabled = false;
                CheckBox12.TextAlign = TextAlign.Right;
                           // TextBox1.Text = (Convert.ToInt32(TextBox1.Text) + MontoMensual).ToString();
                if (CheckBox1.Text == "Enero" && CheckBox14.Checked == false)
                {
                    TextBox1.Text = ((12 * MontoMensualDescuento)).ToString();
                    Label7.Text = "Se esta accidiendo al descuento de Pronto Pago";
                    Label7.Visible = true;
                }
                else
                {
                    if (CheckBox1.Text == "Enero" && CheckBox14.Checked == true)
                    {
                        TextBox1.Text = ((12 * MontoMensualDescuento) + (MontoAnual)).ToString();
                        Label7.Text = "Se esta accidiendo al descuento de Pronto Pago";
                        Label7.Visible = true;
                    }
                    else
                    {
                        if (CheckBox14.Checked == true)
                        {
                            TextBox1.Text = (MontoAnual + (12 * MontoMensual)).ToString();
                        }
                        else
                        {
                            TextBox1.Text = (12 * MontoMensual).ToString();
                        }
                    }
                }
            }
        }

        protected void CheckBox13_CheckedChanged(object sender, EventArgs e)
        {
            
            if (CheckBox13.Checked==true)
            {
                

                if (CheckBox1.Text == "Enero" && CheckBox14.Checked == false)
                {
                    TextBox1.Text = ((12 * MontoMensualDescuento)).ToString();
                    Label7.Text = "Se esta accidiendo al descuento de Pronto Pago";
                    Label7.Visible = true;
                }
                else
                {
                    if (CheckBox1.Text == "Enero" && CheckBox14.Checked == true)
                    {
                        TextBox1.Text = ((12 * MontoMensualDescuento) + (MontoAnual)).ToString();
                        Label7.Text = "Se esta accidiendo al descuento de Pronto Pago";
                        Label7.Visible = true;
                    }
                    else
                    {
                        if (CheckBox14.Checked == true)
                        {
                            TextBox1.Text = (MontoAnual + (12 * MontoMensual)).ToString();
                        }
                        else
                        {
                            TextBox1.Text = (12 * MontoMensual).ToString();
                        }
                    }
                }
                
                CheckBox1.Checked = true;
                CheckBox2.Checked = true;
                CheckBox3.Checked = true;
                CheckBox4.Checked = true;
                CheckBox5.Checked = true;
                CheckBox6.Checked = true;
                CheckBox7.Checked = true;
                CheckBox8.Checked = true;
                CheckBox9.Checked = true;
                CheckBox10.Checked = true;
                CheckBox11.Checked = true;
                CheckBox12.Checked = true;
                CheckBox1.TextAlign = TextAlign.Right;
                CheckBox2.TextAlign = TextAlign.Right;
                CheckBox3.TextAlign = TextAlign.Right;
                CheckBox4.TextAlign = TextAlign.Right;
                CheckBox5.TextAlign = TextAlign.Right;
                CheckBox6.TextAlign = TextAlign.Right;
                CheckBox7.TextAlign = TextAlign.Right;
                CheckBox8.TextAlign = TextAlign.Right;
                CheckBox9.TextAlign = TextAlign.Right;
                CheckBox10.TextAlign = TextAlign.Right;
                CheckBox11.TextAlign = TextAlign.Right;
                CheckBox12.TextAlign = TextAlign.Right;
                CheckBox1.Enabled = false;
                CheckBox2.Enabled = false;
                CheckBox3.Enabled = false;
                CheckBox4.Enabled = false;
                CheckBox5.Enabled = false;
                CheckBox6.Enabled = false;
                CheckBox7.Enabled = false;
                CheckBox8.Enabled = false;
                CheckBox9.Enabled = false;
                CheckBox10.Enabled = false;
                CheckBox11.Enabled = false;
                CheckBox12.Enabled = false;
            }
            if (CheckBox13.Checked==false)
            {
                Label7.Visible = false;
                if (CheckBox14.Checked == false)
                {
                    TextBox1.Text = "0";
                }
                else
                {
                    if ( CheckBox14.Checked == true)
                    {
                        TextBox1.Text = (MontoAnual).ToString();
                    }
                }

                CheckBox1.Checked = false;
                CheckBox2.Checked =false;
                CheckBox3.Checked = false;
                CheckBox4.Checked = false;
                CheckBox5.Checked = false;
                CheckBox6.Checked =false;
                CheckBox7.Checked = false;
                CheckBox8.Checked = false;
                CheckBox9.Checked = false;
                CheckBox10.Checked = false;
                CheckBox11.Checked = false;
                CheckBox12.Checked = false;
                CheckBox1.TextAlign = TextAlign.Left;
                CheckBox1.Enabled = true;
            }
        }

        protected void CheckBox14_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBox14.Checked==true)
            {
                TextBox1.Text = (Convert.ToInt32(TextBox1.Text) + MontoAnual).ToString();    
            }
            else
                TextBox1.Text = (Convert.ToInt32(TextBox1.Text) - MontoAnual).ToString();
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            int res = verificarCheck();
            if (res > 0 || CheckBox14.Checked == true)
            {
                Panel1.Enabled = false;
                //Panel2.Enabled = false;
                Panel3.Enabled = true;
                Panel1.BackColor = Color.White;
                //Panel2.BackColor = Color.White;
                Panel3.BackColor = Color.Silver;


                //TextBox1.Text = (res * 40).ToString();
            }
            else
            {
                Label3.Text = "Seleccionar un item";
                Label3.Visible = true;
                Label1.Visible=false;
            }
            
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            CheckBox13.Checked = false;
            Label7.Visible = false;
            int res = verificarCheck();

            if (CheckBox1.Text == "Enero" && res==12 && CheckBox14.Checked ==false)
            {
                res = 15;
                CheckBox12.Checked = false;
                CheckBox12.Enabled = true;
                CheckBox12.TextAlign = TextAlign.Left;

                TextBox1.Text = (( 12* MontoMensual )-MontoMensual).ToString();
            }
            else
            {
                if (CheckBox1.Text == "Enero" && res==12 && CheckBox14.Checked ==true)
                {
                    res = 15;
                    CheckBox12.Checked = false;
                    CheckBox12.Enabled = true;
                    CheckBox12.TextAlign = TextAlign.Left;

                    TextBox1.Text = (MontoAnual+ ((12 * MontoMensual) - MontoMensual)).ToString();
                }
            }

            if (res==1)
            {
                CheckBox2.TextAlign = TextAlign.Right;
                CheckBox2.Enabled=false;
                CheckBox1.Checked =false;
                CheckBox1.Enabled = true;
                CheckBox1.TextAlign = TextAlign.Left;
                TextBox1.Text = (Convert.ToInt32(TextBox1.Text) - MontoMensual).ToString();
            }
            if (res == 2)
            {
                CheckBox3.TextAlign = TextAlign.Right;
                CheckBox3.Enabled = false;
                CheckBox2.Checked = false;
                CheckBox2.Enabled = true;
                CheckBox2.TextAlign = TextAlign.Left;
                TextBox1.Text = (Convert.ToInt32(TextBox1.Text) - MontoMensual).ToString();
            }
            if (res == 3)
            {
                CheckBox4.TextAlign = TextAlign.Right;
                CheckBox4.Enabled = false;
                CheckBox3.Checked = false;
                CheckBox3.Enabled = true;
                CheckBox3.TextAlign = TextAlign.Left;
                TextBox1.Text = (Convert.ToInt32(TextBox1.Text) - MontoMensual).ToString();
            }
            if (res == 4)
            {
                CheckBox5.TextAlign = TextAlign.Right;
                CheckBox5.Enabled = false;
                CheckBox4.Checked = false;
                CheckBox4.Enabled = true;
                CheckBox4.TextAlign = TextAlign.Left;
                TextBox1.Text = (Convert.ToInt32(TextBox1.Text) - MontoMensual).ToString();
            }
            if (res == 5)
            {
                CheckBox6.TextAlign = TextAlign.Right;
                CheckBox6.Enabled = false;
                CheckBox5.Checked = false;
                CheckBox5.Enabled = true;
                CheckBox5.TextAlign = TextAlign.Left;
                TextBox1.Text = (Convert.ToInt32(TextBox1.Text) - MontoMensual).ToString();
            }
            if (res == 6)
            {
                CheckBox7.TextAlign = TextAlign.Right;
                CheckBox7.Enabled = false;
                CheckBox6.Checked = false;
                CheckBox6.Enabled = true;
                CheckBox6.TextAlign = TextAlign.Left;
                TextBox1.Text = (Convert.ToInt32(TextBox1.Text) - MontoMensual).ToString();
            }
            if (res == 7)
            {
                CheckBox8.TextAlign = TextAlign.Right;
                CheckBox8.Enabled = false;
                CheckBox7.Checked = false;
                CheckBox7.Enabled = true;
                CheckBox7.TextAlign = TextAlign.Left;
                TextBox1.Text = (Convert.ToInt32(TextBox1.Text) - MontoMensual).ToString();
            }
            if (res == 8)
            {
                CheckBox9.TextAlign = TextAlign.Right;
                CheckBox9.Enabled = false;
                CheckBox8.Checked = false;
                CheckBox8.Enabled = true;
                CheckBox8.TextAlign = TextAlign.Left;
                TextBox1.Text = (Convert.ToInt32(TextBox1.Text) - MontoMensual).ToString();
            }
            if (res == 9)
            {
                CheckBox10.TextAlign = TextAlign.Right;
                CheckBox10.Enabled = false;
                CheckBox9.Checked = false;
                CheckBox9.Enabled = true;
                CheckBox9.TextAlign = TextAlign.Left;
                TextBox1.Text = (Convert.ToInt32(TextBox1.Text) - MontoMensual).ToString();
            }
            if (res == 10)
            {
                CheckBox11.TextAlign = TextAlign.Right;
                CheckBox11.Enabled = false;
                CheckBox10.Checked = false;
                CheckBox10.Enabled = true;
                CheckBox10.TextAlign = TextAlign.Left;
                TextBox1.Text = (Convert.ToInt32(TextBox1.Text) - MontoMensual).ToString();
            }
            if (res == 11)
            {
                CheckBox12.TextAlign = TextAlign.Right;
                CheckBox12.Enabled = false;
                CheckBox11.Checked = false;
                CheckBox11.Enabled = true;
                CheckBox11.TextAlign = TextAlign.Left;
                TextBox1.Text = (Convert.ToInt32(TextBox1.Text) - MontoMensual).ToString();
            }
            if (res == 12)
            {
                //CheckBox3.TextAlign = TextAlign.Right;
                //CheckBox3.Enabled = false;
                CheckBox12.Checked = false;
                CheckBox12.Enabled = true;
                CheckBox12.TextAlign = TextAlign.Left;
                TextBox1.Text = (Convert.ToInt32(TextBox1.Text) - MontoMensual).ToString();
            }
            
        }

        protected void Button6_Click(object sender, EventArgs e)
        {
            int res = verificarCheck();
            if (res > 0 || CheckBox14.Checked == true)
            {
                Panel1.Enabled = false;
                //Panel2.Enabled = false;
                Panel3.Enabled = true;
                Panel1.BackColor = Color.White;
                //Panel2.BackColor = Color.White;
                Panel3.BackColor = Color.Silver;



                int aux = verificarCheck();
                string aux2 = "";
                if (CheckBox14.Checked == true)
                {
                    if (anual!=null)
                    {
                        aux2 = (anual.indice + 1).ToString();    
                    }
                    else
                    {
                        aux2 = (DateTime.Now.Year).ToString();    
                    }
                }
                Processes.Cuotas.GestorCuotas.realizarPago(mensual.carnet, aux.ToString(), aux2, mensual.indice);
                Response.Redirect("~/Miembro/Buscar.aspx");

            }
            else
            {
                Label1.Text = "Seleccionar un item";
                Label1.Visible = true;
                Label3.Visible = false;
            }
            
        }
     }
}
